import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: GodK
 * Date: 2024-06-01
 * Time: 16:54
 */
public class Mysqladd {
    public void add(int id, String name) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/practice?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123");
        Connection connection = dataSource.getConnection();
        String sql = "insert into student values(?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);
        statement.setString(2,name);
        int n = statement.executeUpdate();
        System.out.println(n);
        statement.close();
        connection.close();
    }
}
